package http

import (
	"anban/utils"
	"crypto/tls"
	"fmt"
	"github.com/astaxie/beego/logs"
	"io/ioutil"
	"net/http"
	"strings"
)

func GetData(url string) (string, error) {
	client := &http.Client{}
	resp, err := client.Get(url)
	if err != nil {
		return "", err
	}
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	return string(body), err
}

func PayRequest(content string) string {
	req, _ := http.NewRequest("POST", "https://113.108.182.3/aipg/ProcessServlet", strings.NewReader(content))
	//req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	req.Header.Set("Content-type", "application/tlt-notify")
	req.Header.Set("Content-length", fmt.Sprintf("%s", len(content)))
	req.Header.Set("Keep-alive", "false")
	tr := &http.Transport{
		TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
	}
	client := &http.Client{Transport: tr}
	resp, err := client.Do(req)
	if err != nil {
		logs.Error("pay request fail:", err)
		return ""
	}
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		logs.Error("can not read from pay response:", err)
		return ""
	}
	body = utils.GbkToUtf8(body)
	return string(body)
}